package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;

import model.AlarmLog;
import model.Set;

import communication.Alarm;
import communication.AlarmListener;

/**
 * LogPanel
 * @author Adrien Ledeul
 * @version 0.1
 */
public class LogPanel extends JPanel implements ActionListener, MouseListener, AlarmListener
{
	private static final long serialVersionUID = 1L;
	
	private Monitor2Panel monitor2Panel;
	private Set set;
	private JList listPanel = new JList();
	private JScrollPane centerPanel = new JScrollPane(listPanel);
	private JPanel buttons;
	private JButton bDetails;
	private JButton bTreated;
	
	private boolean treated;
	private String type;
	private ArrayList<AlarmLog> log = new ArrayList<AlarmLog>();
	private ArrayList<AlarmLog> logT;
	
	/**
	 * Constructors
	 * 
	 */
	public LogPanel()
	{
		super();
		this.build();
	}
	
	public LogPanel(Monitor2Panel monitor2Panel, String type, boolean treated)
	{
		super();
		this.monitor2Panel = monitor2Panel;
		this.set = monitor2Panel.getSet();
		this.treated = treated;
		this.type = type;
		
		set.getFireSensor().addListener(this);
		set.getGasSensor().addListener(this);
		set.getRadiationSensor().addListener(this);
		this.build();
	}

	/**
	 * Getters & Setters
	 * 
	 */
	public Monitor2Panel getMonitor2Panel()
	{
		return monitor2Panel;
	}

	/**
	 * Build the panel
	 */
	private void build()
	{
		this.setLayout(new BorderLayout());
		this.setFocusable(true);
		
		String SType = "";
		if(this.treated)
			SType = "Treated";
		else
			SType = "Untreated";

		this.setBorder(new TitledBorder(null, "Monitor " + type + " - " + SType, TitledBorder.LEFT, TitledBorder.DEFAULT_POSITION));

		buttons = new JPanel();
		buttons.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		bDetails = new JButton("Details");
		bTreated = new JButton("Treated");
		bDetails.addActionListener(this);
		bTreated.addActionListener(this);
		
		buttons.add(bDetails);
		if(! this.treated)
			buttons.add(bTreated);
		
		
		
		this.updateLog();

		this.add(buttons, BorderLayout.SOUTH);
	}
	
	/**
	 * Update the log display
	 */	
	public void updateLog()
	{
		if(type == "A")
		{
			logT = set.getMonitorA().getLog();
		}
		else if(type == "B")
		{
			logT = set.getMonitorB().getLog();
		}
		
		log = new ArrayList<AlarmLog>();
		for(int i=0; i<logT.size(); i++)
		{
			if(this.treated && logT.get(i).isTreated())
				log.add(logT.get(i));
			else if((!this.treated) && (!logT.get(i).isTreated()))
				log.add(logT.get(i));
		}

		Vector<String> listeItems = new Vector<String>();
		listeItems.removeAllElements();

		for(int i=0; i<log.size(); i++)
		{
			listeItems.addElement(log.get(i).getShortA());
		}
		

		listPanel.setListData(listeItems);
		listPanel.addMouseListener(this);
		
		this.add(centerPanel, BorderLayout.CENTER);
		
		this.updateButtons();
	}
	
	/**
	 * Update the status of buttons
	 */	
	private void updateButtons()
	{
		int index = listPanel.getSelectedIndex();
		
		if(index != -1)
		{
			if(log.get(index).isViewed())
			{
				bTreated.setEnabled(true);
				bDetails.setEnabled(true);
			}
			else
			{
				bTreated.setEnabled(false);
				bDetails.setEnabled(true);
			}
		}
		else
		{
			bTreated.setEnabled(false);
			bDetails.setEnabled(false);
		}
	}

	/**
	 * Alarm receiver
	 * @param alarm Alarm
	 */	
	@Override
	public void receiveAlarm(Alarm alarm) 
	{	
		this.updateLog();
		this.updateUI();
	}
	
	/**
	 * Action listener
	 * @param e ActionEvent
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{	
        if(e.getSource() == bTreated)
        {
        	int index = listPanel.getSelectedIndex();
        	log.get(index).setTreated(true);
        	monitor2Panel.getTreatedPanel().updateLog();
        	monitor2Panel.getUntreatedPanel().updateLog();
        }
        
        if(e.getSource() == bDetails)
        {
        	int index =  listPanel.getSelectedIndex();
        	log.get(index).setViewed(true);
        	JOptionPane.showMessageDialog(null, log.get(index).getLongA(), "Details", JOptionPane.INFORMATION_MESSAGE);
        	monitor2Panel.getTreatedPanel().updateLog();
        	monitor2Panel.getUntreatedPanel().updateLog();
        }
		
	}

	@Override
	public void mouseClicked(java.awt.event.MouseEvent e) 
	{
		this.updateButtons();
	}

	@Override
	public void mouseEntered(java.awt.event.MouseEvent e) 
	{
		
	}

	@Override
	public void mouseExited(java.awt.event.MouseEvent e) 
	{
		
	}

	@Override
	public void mousePressed(java.awt.event.MouseEvent e) 
	{
		
	}

	@Override
	public void mouseReleased(java.awt.event.MouseEvent e) 
	{
		
	}
}
